//
//  User.m
//  iengage
//
//  Created by Luis Ziliani on 2/9/09.
//  Copyright 2009 Manas. All rights reserved.
//

#import "User.h"
#import "Util.h"

@implementation User

@synthesize login;
@synthesize displayName;
@synthesize lastMessage;
@synthesize lastMessageDate;
@synthesize latitude;
@synthesize longitude;
@synthesize lastDistance;
@synthesize imageData;
@synthesize lastDirectionAngle;

- (id) init {
	self = [super init];
	lastDistance = UserDistanceNotCalculated;
	return self;
}

- (CLLocation *) getLocation {
	CLLocation * loc = [[CLLocation alloc] initWithLatitude:self.latitude longitude:self.longitude];
	
	return [loc autorelease];
}

- (NSString*) lastMessageDateDifferenceFromNow {
	if ([NSString isNullOrEmpty:lastMessage] || lastMessageDate == nil) {
		return @"";
	}
	
	NSDate *now = [NSDate date];
	NSTimeInterval interval = [now timeIntervalSinceDate:lastMessageDate];

	NSMutableString *string = [[NSMutableString alloc] init];
	
	if (interval < 0.0) {
		[string appendString:@"in the future!"];
	}
	else if (interval < 60.0) {
		[string appendString:@"less than a minute ago"];
	}
	else if (interval < 60.0*60.0) {
		[string appendFormat:@"%d mins ago", (int)(interval/60.0)];
	}
	else if (interval < 60.0*60.0*24.0) {
		[string appendFormat:@"%d hs ago", (int)(interval/(60.0*60.0))];
	}
	else if (interval < 60.0*60.0*24.0*30.0) {
		[string appendFormat:@"%d days ago", (int)(interval/(60.0*60.0*24.0))];
	}
	else if (interval < 60.0*60.0*24.0*7.0*30.0*12.0) {
		[string appendFormat:@"%d months ago", (int)(interval/(60.0*60.0*24.0*30.0))];
	}
	else  {
		[string appendFormat:@"%d years ago", (int)(interval/(60.0*60.0*24.0*30.0*12.0))];
	}
	[string autorelease];
	return string;
}


- (double) calculateDirectionFrom: (CLLocation *) loc1 to: (CLLocation *)loc2 {
	double lat1 = [loc1 coordinate].latitude * M_PI / 180.0;
	double lat2 = [loc2 coordinate].latitude * M_PI / 180.0;
	double lon1 = [loc1 coordinate].longitude * M_PI / 180.0;
	double lon2 = [loc2 coordinate].longitude * M_PI / 180.0;
	
	/*
	double y = sin(lon2-lon1)*cos(lat2);
	double x = cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(lon2-lon1);
	double tc1;
	
	if (y > 0.0) {
		if (x > 0.0) 
			tc1 = atan2(y/x);
		else if (x < 0.0) 
			tc1 = M_PI - atan2(-y/x);
		else if (x == 0.0)
			tc1 = M_PI/2.0;
	}
	else if (y < 0.0) {
		if (x > 0.0) 
			tc1 = -atan2(-y/x);
		else if (x < 0.0) 
			tc1 = atan2(y/x)-M_PI;
		else if (x == 0.0) 
			tc1 = M_PI*3.0/2.0;
	}
	else if (y == 0.0) {
		if (x > 0.0) 
			tc1 = 0;
		else if (x < 0.0) 
			tc1 = M_PI;
		else if (x == 0.0)
			tc1 = 0;
			//[the 2 points are the same];
	}
	 */
	double tc = fmod(atan2(sin(lon2-lon1)*cos(lat2),
					cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(lon2-lon1)),
					2*M_PI);	
	return tc * 180.0 / M_PI;
}

- (void) calculateDistanceFrom: (CLLocation *)location {
	CLLocationDistance distance;
	double direction = 0.0;
	if (location == nil) {
		distance = UserDistanceNotCalculated;
	}
	else {
		CLLocation * selfLocation = [self getLocation];
		distance = [selfLocation getDistanceFrom:location];
		direction = [self calculateDirectionFrom:location to:selfLocation];
	}
	[self setLastDistance: distance];
	[self setLastDirectionAngle: direction];
}


- (void) dealloc {
	[displayName release];
	[lastMessage release];
	[lastMessageDate release];
	[login release];
	[imageData release];
	[super dealloc];
}
@end
